#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int cmp_int(const void* p1, const void* p2)
{
	//return *(int*)p1 - *(int*)p2;//升序
	return *(int*)p2 - *(int*)p1;//降序
}
//排序整型数组
void test1()
{
	int arr[10] = { 1,3,5,7,9,2,4,6,8,10 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	qsort(arr, sz, sizeof(arr[0]), cmp_int);
}
int cmp_str(const void* p1, const void* p2)
{
	return strcmp((char*)p1, (char*)p2);//升序
	//return strcmp(p2,p1);//降序
}
//排序字符数组
void test2()
{
	char arr[] = "sjhfaskj";
	int len = strlen(arr);
	qsort(arr, len, sizeof(arr[0]), cmp_str);
}
//排序结构体数组
struct stu
{
	char name[20];
	int age;
};
int cmp_stu_name(const void* p1, const void* p2)
{
	//return strcmp((struct stu*)p1, (struct stu*)p2);//升序
	return strcmp(((struct stu*)p2)->name, ((struct stu*)p1)->name);//降序

}
int cmp_stu_age(const void* p1, const void* p2)
{
	return ((struct stu*)p1)->age - ((struct stu*)p2)->age;//升序
	//return ((struct stu*)p2)->age - ((struct stu*)p1)->age;//降序
}
void test3()
{
	struct stu arr[3] = { {"zhangsan",20},{"lisi",50},{"wangwu",15} };
	int sz = sizeof(arr) / sizeof(arr[0]);
	//qsort(arr, sz, sizeof(arr[0]), cmp_stu_age);//按照年龄
	qsort(arr, sz, sizeof(arr[0]), cmp_stu_name);

}
int main()
{
	//test1();//测试qsort排序整型数组
	//test2();//测试qsort排序字符
	test3();//测试qsort排序结构体
	return 0;
 }